<?php
class JobsManageController extends AdminController
{
    public Function actionMain() {
        $page = isset($_REQUEST['p']) ? $_REQUEST['p'] : 1;
        $dataPerpage = 10;
        $MaxPage  = floor(JobsManage::GetMaxPosition()/$dataPerpage) + 1;
        //$jobslist = JobsManage::GetDataPerPage($page, $dataPerpage);
//        if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST" ) {
//            $jobslist = JobsManage::find($_POST['key'], $_POST['col']);
//        }
        if(isset($_GET['Keyword']) || isset($_GET['JobID']) || isset($_GET['Province'])){
            $Keyword = $_GET['Keyword'];
            $JobID = $_GET['JobID'];
            $Province = $_GET['Province'];
            $jobslist = JobsManage::getJobslistByKeyword($Keyword, $JobID, $Province, $page, $dataPerpage, true);
        } else {
            $jobslist = JobsManage::getJobslist($page, $dataPerpage, 'en', true);
        }
        
        $this->render(  'index', 
                array(  'data' => $jobslist, 
                        'page' => $page, 
                        'dataPerpage' => $dataPerpage,
                        'MaxPage' => $MaxPage  ));
    }
    
    public function actionJobFunction() {
        $this->render('jobFunction');
    }
    
    public function actionAdd() {
        if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST" ) {
            if(JobsManage::insertJob($_POST) == 1) {
                $this->redirect("index.php?r=JobsManage/Main");    
            }
            print("Position has not been save");
        }
        $this->redirect("index.php?r=JobsManage/Main");
    }
    
    public function actionEdit() {
        if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST" ) {
            JobsManage::updateJob($_POST,$_POST['jobID']);
            $this->redirect("index.php?r=JobsManage/Main");
        }
        $this->redirect("index.php?r=JobsManage/Main");
    }
    
    public function actionDelete() {
        if(isset($_POST) && $_SERVER['REQUEST_METHOD'] == "POST" ) {
            $id = $_POST['jobID'];
            if(JobsManage::deleteJOB($id)) {
                $this->redirect("index.php?r=JobsManage/Main");
            }
            printf("Cannot delete job id \"%s\"",$id);
        }
        $this->redirect("index.php?r=JobsManage/Main");
    }
    
    public function actionStatus() {
        if (isset($_POST)  && $_SERVER['REQUEST_METHOD'] == "POST" ) {
            $ID = $_POST['JobID'];
            $Status = $_POST['Status'];
            echo JobsManage::editStatusJob($ID, $Status);
        }
    }
    
    public function actionChangeUrgent() {
        if (isset($_POST)  && $_SERVER['REQUEST_METHOD'] == "POST" ) {
            $ID = $_POST['JobID'];
            $Urgent = $_POST['Urgent'];
            echo JobsManage::changeUrgent($ID, $Urgent);
        }
    }
    
}

